package simuspec;
import java.io.BufferedReader;
import java.io.FileReader;

/*
 * J is K_cterm
 * Z is R_cterm
 */

public class Read_Weca_Result {
	double[][] adjust_factor_YY = new double[10][2];
	double[][] adjust_factor_BY = new double[10][2];
	//adjust_factor[0] is the slope;
	//adjuest_factor[1] is the intercept;
	double[][][]  factors_AA_YY = new double[20][100][200];
	double[][][]  factors_AA_BY = new double[20][100][200];
	double[][] factor_K_Cterm_YY = new double[20][200];
	double[][] factor_R_Cterm_YY = new double[20][200];
	double[][] factor_K_Cterm_BY = new double[20][200];
	double[][] factor_R_Cterm_BY = new double[20][200];

	double[] factor_b_YY = new double[10];
	double[] factor_Nterm_YY =new double[10];
	double[] factor_b_BY = new double[10];
	double[] factor_Nterm_BY =new double[10];

	Read_Weca_Result(String weca_result){

		try {
			
		      BufferedReader sq = new BufferedReader(new FileReader(
		    		  weca_result));
		      String a = sq.readLine();
		     
		      while (a!=null&&a.contains("log_ratio =")) {
		    	  //System.out.println(a);
		    	  a = sq.readLine();
		    	  double slope = Double.parseDouble(a);
		    	  a= sq.readLine();
		    	  double intercp= Double.parseDouble(a);
		    	  a = sq.readLine();
		    	  String  type_pos = trim_(a);
		    	  String[] type_posS= type_pos.split("=");
		    	  int pos = Integer.parseInt(type_posS[1].trim());
		    	  if(type_posS[0].equals("Y")){
			    	  adjust_factor_YY[pos][0]= slope;
			    	  adjust_factor_YY[pos][1]= intercp;

		    		  a = sq.readLine();
		    		  factor_b_YY[pos]=Double.parseDouble(trim_(a));
		    		  a = sq.readLine();
		    		  //a = sq.readLine();
				      String[] N = a.trim().split(" ");
				      factor_Nterm_YY[pos] = Double.parseDouble(N[0]);
				      a = sq.readLine();
		    		  while (a!=null&&!a.equals("log_ratio =")) {
				    	  String para = trim_(a);
				    	  String[] paraS= para.split(" ");	
				    	  double ai = Double.parseDouble(paraS[0]);
				    	  //System.out.println("paraS[2]..."+paraS[2]);
				    	  String[] ap = paraS[2].split("_");
				    	  int AA_x = Integer.parseInt(ap[1]);
				    	  char aa = ap[0].charAt(0);
				    	  if(aa=='J'){
				    		  for(int u=AA_x;u<this.factor_K_Cterm_YY[pos].length;u++){
				    			  
				    			  this.factor_K_Cterm_YY[pos][u]=ai ;
				    		  
				    		  }
				    	  }
				    	  else if(aa=='Z'){
				    		  for(int u=AA_x;u<this.factor_R_Cterm_YY[pos].length;u++){
				    			  this.factor_R_Cterm_YY[pos][u]=ai ;
				    		  
				    		  }

				    		  
				    	  }
				    	  else{
				    		  int AA_y = Character.getNumericValue(aa);
				    		  factors_AA_YY[pos][AA_y][AA_x]= ai;
				    	  }
				    	  
				    	  
				    	  a = sq.readLine();
				      }
		    	  }
		    	  if(type_posS[0].equals("B")){
			    	  adjust_factor_BY[pos][0]= slope;
			    	  adjust_factor_BY[pos][1]= intercp;

		    		  a = sq.readLine();
		    		  factor_b_BY[pos]=Double.parseDouble(trim_(a));
		    		  a = sq.readLine();
				      String[] N = a.trim().split(" ");
				      factor_Nterm_BY[pos] = Double.parseDouble(N[0]);
				      a = sq.readLine();
		    		  while (a!=null&&!a.equals("log_ratio =")) {
				    	  String para = trim_(a);
				    	  String[] paraS= para.split(" ");	
				    	  double ai = Double.parseDouble(paraS[0]);
				    	  //System.out.println("paraS[2]..."+paraS[2]);
				    	  String[] ap = paraS[2].split("_");
				    	  //System.out.println(ap[0]+ap[1]);
				    	  char aa = ap[0].charAt(0);
				    	  int AA_x = Integer.parseInt(ap[1]);
				    	  //System.out.println((char)(AA_y+55)+"\t"+AA_x);
				    	  if(aa=='J'){
				    		  for(int u=AA_x;u<this.factor_K_Cterm_BY[pos].length;u++){
				    			  this.factor_K_Cterm_BY[pos][u]=ai ;
				    		  
				    		  }

					    		
				    	  }
				    	  else if(aa=='Z'){
				    		  for(int u=AA_x;u<this.factor_R_Cterm_BY[pos].length;u++){
				    			  this.factor_R_Cterm_BY[pos][u]=ai ;
				    		  
				    		  }

				    		 
				    	  }
				    	  else{
				    		  int AA_y = Character.getNumericValue(aa);
				    		  factors_AA_BY[pos][AA_y][AA_x]= ai;
				    	  }
				    	  a = sq.readLine();
				      }
		    	  }
		      }
		      sq.close();
	    }catch (Exception e) {
	      System.out.println("Sorry, the type of file " + " isn't correct");
	      e.printStackTrace();
	    }
	    //publicMethod.ArrayManipulation.printArray(this.factors_AA_YY[7][Character.getNumericValue('R')]);
	   
	   // System.out.println("factor_b..."+factor_b_YB);
	    //System.out.println("factor_N..."+factor_Nterm_YB);
/*	    
	    for(int i=0;i< factors_AA_YY.length;i++){
	    	for(int j=0;j<factors_AA_YY[0].length;j++){
	    		for(int k=0;k<factors_AA_YY[0][0].length;k++){
	    			if(factors_AA_YY[i][j][k]!=0){
	    				System.out.println(i+"\t"+(char)(j+55)+"\t"+k+"\t"+factors_AA_YY[i][j][k]);
	    			}
	    		}
	    	}
	    }
	    */
	    /*
	    for(int j=0;j<factors_AA_YY[0].length;j++){
	    	for(int i=1;i<50;i++){
	    	
	    		System.out.print((char)(j+55)+"_"+i+"\t");
	    		if(j==Character.getNumericValue('A')||j==Character.getNumericValue('P')){
		    		for(int k=0;k<factors_AA_YY.length;k++){
		    			System.out.print(factors_AA_YY[k][j][i]+"\t");
		    			
		    		}
		    		System.out.println();
	    		}
	    	}
	    }
	    */
	    
	    for(int i=4;i<Math.min(5,factors_AA_YY.length);i++){
	    	for(int j=1;j<factors_AA_YY[0].length;j++){
	    		//System.out.print((char)(j+55)+"_"+i+"\t");
	    		for(int k=0;k<factors_AA_YY[0][0].length;k++){
	    			//System.out.print(factors_AA_YY[i][j][k]+"\t");
			    			
	    		}
	    		//System.out.println();
	    	}
	    }
	}
	String trim_(String a){
		String para = a.trim();
		StringBuffer result = new StringBuffer();
		String[] paraS= para.split(" ");
		for(int i=0;i<paraS.length;i++){
			if(paraS[i].equals(""));
			else{
				result.append(paraS[i]+" ");
			}
		}
		return result.toString();
	}
}

